//
//  HJKeychain.m
//  KaiChat
//
//  Created by lx on 2023/11/27.
//

#import "HJKeychain.h"
#import "SSKeychain.h"


NSString * const appleUserIdKey = @"appleUserId";
NSString * const appleUserEmailKey = @"appleUserEmail";
NSString * const appleUserFullNameKey = @"appleUserFullName";
NSString * const appleUserIdentityTokenKey = @"appleUserIdentityToken";
NSString * const appleUserRealUserStatusKey = @"appleUserRealUserStatus";
NSString * const appleUserAuthorizationCodeKey = @"appleUserAuthorizationCode";
NSString * const appleServiceName = @"com.Hj.KaiChat";
NSString * const UDIDKey = @"accountKey";

@implementation HJKeychain


+ (BOOL)setObject:(NSString *)Object forKey:(NSString *)key {
    return [SSKeychain setPassword:Object forService:appleServiceName account:key];
}

+ (NSString *)objectForKey:(NSString *)key {
    return [SSKeychain passwordForService:appleServiceName account:key];
}

+ (BOOL)deleteForKey:(NSString *)key {
    return [SSKeychain deletePasswordForService:appleServiceName account:key];
}

+ (void)setUDID:(NSString *)UDID {
    [SSKeychain setPassword:UDIDKey forService:appleServiceName account:UDID];
}
 
+ (NSString *)UDID {
    return [SSKeychain passwordForService:appleServiceName account:UDIDKey];
}
 
+ (void)setAppleUserId:(NSString *)appleUserId {
    [SSKeychain setPassword:appleUserId forService:appleServiceName account: appleUserIdKey];
}

+ (NSString *)appleUserId {
    return [SSKeychain passwordForService:appleServiceName account:appleUserIdKey];
}

+ (void)setAppleUserEmail:(NSString *)appleUserEmail {
    if ([self isEmptyString:appleUserEmail]) return;
    [SSKeychain setPassword:appleUserEmail forService:appleServiceName account:appleUserEmailKey];
}

+ (NSString *)appleUserEmail {
    return [SSKeychain passwordForService:appleServiceName account:appleUserEmailKey];
}

+ (void)setAppleUserFullName:(NSString *)appleUserFullName {
    if ([self isEmptyString:appleUserFullName]) return;
    [SSKeychain setPassword:appleUserFullName forService:appleServiceName account:appleUserFullNameKey];
}

+ (NSString *)appleUserFullName {
    return [SSKeychain passwordForService:appleServiceName account:appleUserFullNameKey];
}

+ (void)setAppleUserIdentityToken:(NSString *)appleUserIdentityToken {
    [SSKeychain setPassword:appleUserIdentityToken forService:appleServiceName account:appleUserIdentityTokenKey];
}

+ (NSString *)appleUserIdentityToken {
    return [SSKeychain passwordForService:appleServiceName account:appleUserIdentityTokenKey];
}

+ (void)setAppleUserRealUserStatus:(NSString *)appleUserRealUserStatus {
    if (!appleUserRealUserStatus) return;
    [SSKeychain setPassword:appleUserRealUserStatus forService:appleServiceName account:appleUserRealUserStatusKey];
}

+ (NSString *)appleUserRealUserStatus {
    return [SSKeychain passwordForService:appleServiceName account:appleUserRealUserStatusKey];
}

+ (void)setAppleUserAuthorizationCode:(NSString *)appleUserAuthorizationCode {
    if (!appleUserAuthorizationCode) return;
    [SSKeychain setPassword:appleUserAuthorizationCode forService:appleServiceName account:appleUserAuthorizationCodeKey];
}

+ (NSString *)appleUserAuthorizationCode {
    return [SSKeychain passwordForService:appleServiceName account:appleUserAuthorizationCodeKey];
}

+ (BOOL)isEmptyString:(NSString *)string {
    
    if (![string isKindOfClass:NSString.class]) return YES;
    if (string == nil || string == NULL) return YES;
    if ([string isEqualToString:@"(null)"]) return YES;
    
    if (string.length == 0) return YES;
    string = [string stringByReplacingOccurrencesOfString:@" " withString:@""];
    
    if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0) {
        return YES;
    }
    return NO;
}




@end
